home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / Halma 1.1.source Folder / Halma ƒ / Halma code ƒ / file management.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-21  |  5.8 KB  |  231 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        file management.c
  4.  
  5. Purpose:    This module handles files for deBinHexing: opening the
  6.             input file, creating/opening the temp file, reading and
  7.             writing files, and finalizing them when we're done.
  8.  
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13.  
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. GNU General Public License for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with this program in a file named "GNU General Public License".
  21. If not, write to the Free Software Foundation, 675 Mass Ave,
  22. Cambridge, MA 02139, USA.
  23.  
  24. \**********************************************************************/
  25.  
  26. #include "Script.h"
  27. #include "file management.h"
  28. #include "environment.h"
  29. #include "util.h"
  30.  
  31. unsigned long    theFileType, theFileCreator;
  32. short            theFileFlags;
  33. unsigned long    theCreationDate, theModificationDate;
  34. FSSpec            inputFS, outputFS, tempFS;
  35. Boolean            deleteBinHexFile;
  36. short            inputRefNum, outputDFRefNum, outputRFRefNum;
  37. unsigned long    outputDFeof, outputRFeof;
  38.  
  39. void InitFiles(void)
  40. {
  41.     inputRefNum=outputDFRefNum=outputRFRefNum=0;
  42.     outputDFeof=outputRFeof=0L;
  43.     deleteBinHexFile=TRUE;
  44. }
  45.  
  46. enum ErrorTypes OpenInputFile(void)
  47. {
  48.     OSErr            isHuman;
  49.     HParamBlockRec    paramBlock;
  50.     
  51.     if (gHasFSSpecs)
  52.         isHuman=FSpOpenDF(&inputFS, fsRdPerm, &inputRefNum);
  53.     else
  54.         isHuman=HOpen(inputFS.vRefNum, inputFS.parID, inputFS.name,
  55.                         fsRdPerm, &inputRefNum);
  56.     if (!isHuman)
  57.     {
  58.         SetFPos(inputRefNum, 1, 0L);
  59.         
  60.         paramBlock.fileParam.ioCompletion=0L;
  61.         paramBlock.fileParam.ioNamePtr=inputFS.name;
  62.         paramBlock.fileParam.ioVRefNum=inputFS.vRefNum;
  63.         paramBlock.fileParam.ioFDirIndex=0;
  64.         paramBlock.fileParam.ioDirID=inputFS.parID;
  65.         PBHGetFInfo(¶mBlock, FALSE);
  66.         theCreationDate=paramBlock.fileParam.ioFlCrDat;
  67.         theModificationDate=paramBlock.fileParam.ioFlMdDat;
  68.     }
  69.     else return kCantOpenInputFile;
  70. }
  71.  
  72. enum ErrorTypes CreateTempFile(void)
  73. {
  74.     short                i;
  75.     Str255                timeStr;
  76.     OSErr                isHuman;
  77.     enum ErrorTypes        resultCode;
  78.     
  79.     tempFS=outputFS;
  80.     tempFS.name[0]=0x04;
  81.     tempFS.name[1]='H';
  82.     tempFS.name[2]='Q';
  83.     tempFS.name[3]='X';
  84.     tempFS.name[4]='.';
  85.     NumToString(Time&0x7fffffff, timeStr);
  86.     for (i=1; i<=timeStr[0]; i++)
  87.         tempFS.name[++(tempFS.name[0])]=timeStr[i];
  88.     
  89.     if (gHasFSSpecs)
  90.         isHuman=FSpCreate(&tempFS, CREATOR, 'TeMp', smSystemScript);
  91.     else
  92.         isHuman=HCreate(tempFS.vRefNum, tempFS.parID, tempFS.name, CREATOR, 'TeMp');
  93.     
  94.     return (isHuman==noErr) ? allsWell : kCantCreateTempFile;
  95. }
  96.  
  97. enum ErrorTypes SetupTempFile(void)
  98. {
  99.     OSErr                isHuman;
  100.     
  101.     if (outputDFeof!=0)
  102.     {
  103.         if (gHasFSSpecs)
  104.             isHuman=FSpOpenDF(&tempFS, fsRdWrPerm, &outputDFRefNum);
  105.         else
  106.             isHuman=HOpenDF(tempFS.vRefNum, tempFS.parID,
  107.                             tempFS.name, fsRdWrPerm, &outputDFRefNum);
  108.     }
  109.     
  110.     if (isHuman!=noErr)
  111.         return kDiskWriteErr;
  112.     
  113.     if (outputRFeof!=0)
  114.     {
  115.         if (gHasFSSpecs)
  116.             isHuman=FSpOpenRF(&tempFS, fsRdWrPerm, &outputRFRefNum);
  117.         else
  118.             isHuman=HOpenRF(tempFS.vRefNum, tempFS.parID,
  119.                             tempFS.name, fsRdWrPerm, &outputRFRefNum);
  120.     }
  121.     
  122.     if (isHuman!=noErr)
  123.         return kDiskWriteErr;
  124.     
  125.     if (outputDFeof!=0)
  126.         isHuman=SetEOF(outputDFRefNum, outputDFeof);
  127.     if ((!isHuman) && (outputRFeof!=0))
  128.         isHuman=SetEOF(outputRFRefNum, outputRFeof);
  129.  
  130.     if (!isHuman)
  131.     {
  132.         FlushVol(0L, tempFS.vRefNum);
  133.         if (outputDFeof!=0)
  134.             SetFPos(outputDFRefNum, 1, 0L);
  135.         if (outputRFeof!=0)
  136.             SetFPos(outputRFRefNum, 1, 0L);
  137.     }
  138.     
  139.     return (isHuman ? kDiskWriteErr : allsWell);
  140. }
  141.  
  142. void FinalizeFiles(Boolean good)
  143. {
  144.     HParamBlockRec        paramBlock;
  145.     FInfo                theInfo;
  146.     
  147.     if (inputRefNum)
  148.         FSClose(inputRefNum);
  149.     
  150.     if (outputDFRefNum)
  151.         FSClose(outputDFRefNum);
  152.     if (outputRFRefNum)
  153.             FSClose(outputRFRefNum);
  154.     
  155.     FlushVol(0L, tempFS.vRefNum);
  156.     
  157.     if (!good)
  158.     {
  159.         if(gHasFSSpecs)
  160.             FSpDelete(&tempFS);
  161.         else
  162.             HDelete(tempFS.vRefNum, tempFS.parID, tempFS.name);
  163.         
  164.         return;
  165.     }
  166.     
  167.     if (deleteBinHexFile)
  168.     {
  169.         if (gHasFSSpecs)
  170.             FSpDelete(&outputFS);
  171.         else
  172.             HDelete(outputFS.vRefNum, outputFS.parID, outputFS.name);
  173.     }
  174.     FlushVol(0L, tempFS.vRefNum);
  175.     
  176.     
  177.     if (gHasFSSpecs)
  178.         FSpGetFInfo(&tempFS, &theInfo);
  179.     else
  180.         HGetFInfo(tempFS.vRefNum, tempFS.parID, tempFS.name,
  181.                     &theInfo);
  182.  
  183.     Mymemset((Ptr)(&theInfo), 0, sizeof(theInfo));
  184.  
  185.     theInfo.fdType=theFileType;
  186.     theInfo.fdCreator=theFileCreator;
  187.     theInfo.fdFlags=theFileFlags;
  188.     theInfo.fdFlags&=~0x0100;    /* clear Inited bit */
  189.     
  190.     if (gHasFSSpecs)
  191.         FSpSetFInfo(&tempFS, &theInfo);
  192.     else
  193.         HSetFInfo(tempFS.vRefNum, tempFS.parID, tempFS.name,
  194.                     &theInfo);
  195.     FlushVol(0L, tempFS.vRefNum);
  196.     
  197.     paramBlock.fileParam.ioCompletion=0L;
  198.     paramBlock.fileParam.ioNamePtr=tempFS.name;
  199.     paramBlock.fileParam.ioVRefNum=tempFS.vRefNum;
  200.     paramBlock.fileParam.ioFDirIndex=0;
  201.     paramBlock.fileParam.ioDirID=tempFS.parID;
  202.     PBHGetFInfo(¶mBlock, FALSE);
  203.     paramBlock.fileParam.ioFlCrDat=theCreationDate;
  204.     paramBlock.fileParam.ioFlMdDat=theModificationDate;
  205.     PBHSetFInfo(¶mBlock, FALSE);    
  206.     FlushVol(0L, tempFS.vRefNum);    
  207.     
  208.     if (gHasFSSpecs)
  209.         FSpRename(&tempFS, outputFS.name);
  210.     else
  211.         HRename(tempFS.vRefNum, tempFS.parID,
  212.                 tempFS.name, outputFS.name);
  213.     
  214.     FlushVol(0L, tempFS.vRefNum);
  215. }
  216.  
  217. enum ErrorTypes ReadInputFile(short thisFile, Ptr buffer, unsigned long count,
  218.     unsigned long *actualCount)
  219. {
  220.     OSErr            isHuman;
  221.     
  222.     *actualCount=count;
  223.     isHuman=FSRead(thisFile, (long*)actualCount, buffer);
  224.     return ((isHuman==eofErr) || (isHuman==noErr)) ? allsWell : kDiskReadErr;
  225. }
  226.  
  227. enum ErrorTypes WriteTempFile(short thatFile, Ptr buffer, unsigned long theLength)
  228. {
  229.     return (FSWrite(thatFile, (long*)(&theLength), buffer)==noErr ? allsWell : kDiskWriteErr);
  230. }
  231.